package stack;

import org.junit.Test;

import java.util.Scanner;

public class Begin {
    public static void main(String[] args) {
        Stack stack = new Stack(5);
        int n;
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("1:展现");
            System.out.println("2:加入数据");
            System.out.println("3:删除数据");
            System.out.println("4:退出");
            n=scanner.nextInt();
            switch (n){
                case 1:
                    System.out.println("展示的数据为：");
                    System.out.println("----------------------------");
                    stack.showStack();
                    System.out.println("----------------------------");
                    break;
                case 2:
                    System.out.println("输入您要添加的数字:");
                    int value = scanner.nextInt();
                    stack.pushData(value);
                    break;
                case 3:
                    System.out.println("删除的数据为:"+stack.popData());;
                    break;
                case 4:
                    scanner.close();
                    System.out.println("程序退出");
                    System.exit(1);
                    break;
            }
        }
    }
    @Test
    public void test(){
        String str = "70*2*2-5+1-5+3-4";
        Stack nums = new Stack(10);
        Stack sizes = new Stack(10);
        int index = 0;
        String str1 ="";
        int num1 = 0;
        int num2 = 0;
        int res = 0;
        char ch= ' ';
        while (true){
            ch=str.substring(index,index+1).charAt(0);
            if(sizes.isSwapSize(ch)){
                if (!sizes.isEmpty()){
                    if(sizes.priority(ch)<=sizes.peek()){
                        char size = (char) sizes.popData();
                        num1=nums.popData();
                        num2=nums.popData();
                        res = nums.swap(num1, num2, size);
                        nums.pushData(res);
                        sizes.pushData(ch);
                    }else {
                        sizes.pushData(ch);
                    }
                }else {
                    sizes.pushData(ch);
                }
            }else {

                str1+= ch;
                if(index == str.length()-1){
                    nums.pushData(Integer.parseInt(str1));
                }else {
                    if(sizes.isSwapSize(str.substring(index+1,index+2).charAt(0))){
                        nums.pushData(Integer.parseInt(str1));
                        str1="";
                    }
                }
            }
            index++;
            if(index>=str.length()){
                break;
            }
        }
        while (true){
            if (sizes.isEmpty()){
                break;
            }
            char i = (char) sizes.popData();
            num1 = nums.popData();
            num2 = nums.popData();
            res = nums.swap(num1,num2,i);
            nums.pushData(res);
        }
        System.out.println("结果为:"+nums.popData());
    }

}

class Stack{
    private int maxSize;
    private int top = -1;
    private int [] stack;

    public Stack(int maxSize) {
        this.maxSize = maxSize;
        stack=new int[maxSize];
    }

    boolean isFull(){
        return maxSize-1==top;
    }

    boolean isEmpty(){
        return top == -1;
    }

    public void pushData(int n){
        if (isFull()){
            System.out.println("栈满无法加入");
            return;
        }
        stack[++top]=n;
    }
    public int popData(){
        if(isEmpty()){
            throw new RuntimeException ("栈空");
        }
//        System.out.println("1123"+top);
        return stack[top--];
    }

    public void showStack() {
        for(int i = top;i>=0;i--){
            System.out.println(stack[i]);
        }
    }
    public int peek(){
        return stack[top];
    }
    public int priority(int oper){
        if(oper=='/' ||oper=='*'){
            return 1;
        }else if(oper == '+'|| oper=='-'){
            return 0;
        }else return -1;
    }
    public boolean isSwapSize(char size){
        return size == '*' || size == '/'||size == '+'|| size == '-';
    }
    public int swap(int num1,int num2,char size){
        int value=0;
        switch (size){
            case '+':
                value=num1+num2;
                break;
            case '-':
                value=num2-num1;
                System.out.println(value);
                break;
            case '*':
                value=num1*num2;
                break;
            case '/':
                value=num2/num1;
        }
//        System.out.println("每次的结果为:"+value);
        return value;

    }
}
